<script setup lang="ts">
import { computed } from 'vue';
import { ITodo } from '../App.vue';

interface Props {
  list: ITodo[];
}

const props = defineProps<Props>();

const emits = defineEmits(['checkFn', 'delFn', 'checkAll']);
const changeFn = (id: number) => {
  emits('checkFn', id);
};

// const isCheckAll = computed(() => {
//   return props.list.length > 0 && props.list.every(v => v.done);
// });

const isCheckAll = computed({
  get() {
    return props.list.length > 0 && props.list.every(v => v.done);
  },
  set(val) {
    emits('checkAll', val);
  },
});
</script>

<template>
  <section class="main">
    <input id="toggle-all" class="toggle-all" type="checkbox" v-model="isCheckAll" />
    <label for="toggle-all">Mark all as complete</label>
    <ul class="todo-list">
      <li :class="{ completed: item.done }" v-for="item in list" :key="item.id">
        <div class="view">
          <input class="toggle" type="checkbox" :checked="item.done" @change="changeFn(item.id)" />
          <label>{{ item.content }}</label>
          <button class="destroy" @click="emits('delFn', item.id)"></button>
        </div>
        <input class="edit" value="Create a TodoMVC template" />
      </li>
    </ul>
  </section>
</template>

<style lang="less" scoped></style>
